# -*- coding: utf-8 -*-

import uuid
from django.apps import apps
from django.db import models
from django.utils.translation import gettext_lazy as _
from taggit.managers import TaggableManager
from taggit.models import GenericUUIDTaggedItemBase, TaggedItemBase
from application import settings

table_prefix = settings.TABLE_PREFIX  # 数据库表名前缀


class UUIDTaggedItem(GenericUUIDTaggedItemBase, TaggedItemBase):
    # 这里不需要添加额外的字段，只是指定 content_type_field 的实现
    class Meta:
        app_label = "taggit"
        verbose_name = _("Tag")
        verbose_name_plural = _("Tags")


class BaseModel(models.Model):
    """
    基础抽象模型
    """

    id = models.UUIDField(
        primary_key=True,
        default=uuid.uuid4,
        editable=False,
        verbose_name="Id",
    )
    creator = models.ForeignKey(
        null=True,
        to=settings.AUTH_USER_MODEL,
        on_delete=models.SET_NULL,
        db_constraint=False,
        related_query_name="creator_query",
        verbose_name="创建人",
    )
    create_time = models.DateTimeField(
        null=True,
        blank=True,
        auto_now_add=True,
        verbose_name="创建时间",
    )
    modifier = models.CharField(
        null=True, blank=True, max_length=256, verbose_name="修改人"
    )
    update_time = models.DateTimeField(
        null=True,
        blank=True,
        auto_now=True,
        verbose_name="修改时间",
    )

    class Meta:
        abstract = True
        verbose_name = "基础抽象模型"
        verbose_name_plural = verbose_name


class SimpleModel(BaseModel):
    """
    简单抽象模型
    """

    dept_belong_id = models.CharField(
        null=True,
        blank=True,
        max_length=255,
        verbose_name="数据归属部门",
    )
    status = models.CharField(default="0", max_length=32, verbose_name="状态")
    sort = models.IntegerField(
        null=True, blank=True, default=1, verbose_name="显示排序"
    )
    remark = models.TextField(null=True, blank=True, verbose_name="备注信息")

    class Meta:
        abstract = True
        verbose_name = "简单抽象模型"
        verbose_name_plural = verbose_name


class CoreModel(SimpleModel):
    """
    核心抽象模型
    """

    prefix = models.IntegerField(default=0, verbose_name="版本序号")
    params = models.JSONField(default=dict, verbose_name="参数")
    tags = TaggableManager(blank=True, verbose_name="标签", through=UUIDTaggedItem)

    class Meta:
        abstract = True
        verbose_name = "核心抽象模型"
        verbose_name_plural = verbose_name


def get_all_models_objects(model_name=None):
    """
    获取所有 models 对象
    :return: {}
    """
    settings.ALL_MODELS_OBJECTS = {}
    if not settings.ALL_MODELS_OBJECTS:
        all_models = apps.get_models()
        for item in list(all_models):
            table = {
                "tableName": item._meta.verbose_name,
                "table": item.__name__,
                "tableFields": [],
            }
            for field in item._meta.fields:
                fields = {"title": field.verbose_name, "field": field.name}
                table["tableFields"].append(fields)
            settings.ALL_MODELS_OBJECTS.setdefault(
                item.__name__, {"table": table, "object": item}
            )
    if model_name:
        return settings.ALL_MODELS_OBJECTS[model_name] or {}
    return settings.ALL_MODELS_OBJECTS or {}
